package org.fintan.make.configs;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableTransactionManagement
public class MybatisConfig implements EnvironmentAware {
	private RelaxedPropertyResolver propertyResolver;

	private String driveClassName;
	private String url;
	private String userName;
	private String password;
	private String xmlLocation;
	// private String typeAliasesPackage;

	@Bean
	public DataSource dataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUrl(this.url);
		druidDataSource.setUsername(this.userName);
		druidDataSource.setDriverClassName(this.driveClassName);
		druidDataSource.setPassword(this.password);

		return druidDataSource;
	}

	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean() {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource());
//		bean.setTypeAliasesPackage("tk.mybatis.springboot.model");
		/*
		 * // 分页插件 PageHelper pageHelper = new PageHelper(); Properties
		 * properties = new Properties(); properties.setProperty("reasonable",
		 * "true"); properties.setProperty("supportMethodsArguments", "true");
		 * properties.setProperty("returnPageInfo", "check");
		 * properties.setProperty("params", "count=countSql");
		 * pageHelper.setProperties(properties);
		 * 
		 * // 添加插件 bean.setPlugins(new Interceptor[]{(Interceptor) pageHelper});
		 */
		// 添加XML目录
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			bean.setMapperLocations(resolver.getResources(xmlLocation));
			return bean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	public void setEnvironment(Environment environment) {
		this.propertyResolver = new RelaxedPropertyResolver(environment, null);

		this.driveClassName = propertyResolver.getProperty("spring.datasource.driver-class-name");
		this.url = propertyResolver.getProperty("spring.datasource.url");
		this.userName = propertyResolver.getProperty("spring.datasource.username");
		this.password = propertyResolver.getProperty("spring.datasource.password");
		// this.typeAliasesPackage =
		// propertyResolver.getProperty("mybatis.typeAliasesPackage");
		this.xmlLocation = propertyResolver.getProperty("mybatis.mapper-locations");
		System.out.println(xmlLocation);
	}

	@Bean
	public DataSourceTransactionManager transactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
}
